﻿namespace etracks.Web.Controllers
{
    using etracks.EntityModels;
    using etracks.Helpers;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Data.Linq;
    using System.Data.Common;

	[Authorize]
	public class ProjectTypeController : BaseController
	{
		public ActionResult Index ()
		{
			return View ();
		}

        public ActionResult List()
        {
            ProjectTypeListViewModel vm = new ProjectTypeListViewModel();

            vm.ProjectTypeList = ProjectTypeController.GetProjectTypeList();

            return View(vm);
        }

        public ActionResult Add()
        {
            return View(new ProjectTypeRegistrationViewModel());
        }

        [HttpPost]
        public ActionResult Add(ProjectTypeRegistrationViewModel vm)
        {
            try
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("name", vm.Name);

                PostgresHelper.Execute("INSERT INTO project_type (name) VALUES (:name);", parameters);

                this.SuccessAlert("Resource type added.");

            }
            catch (Exception)
            {
                throw;
            }

            return View(vm);
        }

        public ActionResult Edit(int id)
        {
            ProjectTypeRegistrationViewModel vm = new ProjectTypeRegistrationViewModel();

            ProjectType p = GetProjectType(id);

            if (p != null)
            {
                vm = new ProjectTypeRegistrationViewModel
                {
                    Id = p.Id,
                    Name = p.Name
                };
            }

            return View(vm);
        }

        [HttpPost]
        public ActionResult Edit(ProjectTypeRegistrationViewModel vm)
        {
            try
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("id", vm.Id);
                parameters.Add("name", vm.Name);

                PostgresHelper.Execute("UPDATE project_type SET name = :name WHERE id = :id;", parameters);

            }
            catch (Exception)
            {
                throw;
            }

            return View(vm);
        }

        ////////////////////////////////////////////////////////////////////////////////
        // Data retrieval hack functions
        private static List<ProjectTypeReadOnlyViewModel> GetProjectTypeList()
        {
            List<ProjectTypeReadOnlyViewModel> result = new List<ProjectTypeReadOnlyViewModel>();

            DataTable dt = PostgresHelper.GetDataTable(@"SELECT * FROM project_type ORDER BY id;");

            if (dt != null)
            {
                result = dt.AsEnumerable().Select(n => new ProjectTypeReadOnlyViewModel
                {
                    Id = n.Field<int>("id"),
                    Name = n.Field<string>("name"),
                }).ToList();
            }

            return result;
        }

        private static ProjectType GetProjectType(int id)
        {
            ProjectType result = new ProjectType();

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("id", id);

            DataTable dt = PostgresHelper.GetDataTable("SELECT * FROM project_type WHERE id = :id ORDER BY id LIMIT 1;", parameters);

            if (dt != null)
            {
                result = dt.AsEnumerable().Select(n => new ProjectType
                {
                    Id = n.Field<int>("id"),
                    Name = n.Field<string>("name")
                }).FirstOrDefault();
            }

            return result;
        }
	}
}